package com.example.news.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.R;
import com.example.news.activity.DetailsActivity;
import com.example.news.bean.TypeDb;
import com.example.news.bean.TypeJson;
import com.example.news.dao.TypeDao;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/5/17.
 */
public class FindFragment extends Fragment {
    private ArrayList<TypeJson.MyData> list;
    private ListView lv;
    private ArrayList<TypeDb> query;
    private TypeDao dao;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //加载布局
        View view = inflater.inflate(R.layout.find_frag, null);
        //找控件
        lv = (ListView) view.findViewById(R.id.find_list);

        dao = new TypeDao(getActivity());

        //请求网络数据
        getData();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), DetailsActivity.class);
                intent.putExtra("title",list.get(position).name);
                startActivity(intent);
            }
        });

        return view;
    }

    private void getData() {
        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();
                httpUtils.send(HttpRequest.HttpMethod.GET, "http://169.254.25.59:8080/yidian/json/type.json", new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        //连接成功，得到数据
                        String json = responseInfo.result;
                        Gson gson = new Gson();
                        TypeJson typeJson = gson.fromJson(json, TypeJson.class);

                        list = typeJson.data;

                        lv.setAdapter(new MyAadpter());
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        query = dao.query();
                        lv.setAdapter(new BaseAdapter() {
                            @Override
                            public int getCount() {
                                return query.size();
                            }

                            @Override
                            public Object getItem(int position) {
                                return null;
                            }

                            @Override
                            public long getItemId(int position) {
                                return 0;
                            }

                            @Override
                            public View getView(int position, View convertView, ViewGroup parent) {
                                View view = View.inflate(getActivity(), R.layout.find_list_item, null);
                                TextView name = (TextView) view.findViewById(R.id.news_name);
                                TextView num = (TextView) view.findViewById(R.id.number);

                                name.setText(query.get(position).getName());
                                num.setText("26.9万人订阅");
                                return view;
                            }
                        });
                        Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();
    }

    /**
     * 自定义适配器
     */
    class MyAadpter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getActivity(), R.layout.find_list_item, null);
            TextView name = (TextView) view.findViewById(R.id.news_name);
            TextView num = (TextView) view.findViewById(R.id.number);
            Button dingyue = (Button) view.findViewById(R.id.dingyue);

            name.setText(list.get(position).name);
            num.setText("26.9万人订阅");
            /**
             * 订阅按钮
             * 实现订阅功能
             */
            dingyue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "暂不支持订阅功能哦", Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }
    }
}
